package com.chencai.wiki.database.googleapp;

import java.util.Date;
import java.util.List;


import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;



@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class TablePage 
{
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
    
    @Persistent
    private String title;
    
    @Persistent
    private String author;
    
    @Persistent
    private Date createDate;
    
    @Persistent
    private Date updateDate;
    
    @Persistent
    private int updateCounts;
       
    @Persistent
    private Text content;

    public TablePage(String title, String author, Date createDate, Text content)
    {
    	this.title = title;
    	this.author = author;
    	this.createDate = createDate;
    	this.content = content;
    	this.updateCounts = 1;
    	this.updateDate = createDate;
    }

    public long getPageID()
    {
    	return key.getId();
    }
    
    public String getTitle()
    {
    	return this.title;
    }
    
    public void setTitle(String title)
    {
    	this.title = title;
    }
        
    public String getAuthor()
    {
    	return this.author;
    }
    
    public void setAuthor(String author)
    {
    	this.author = author;
    }
    
    public Date getCreateDate()
    {
    	return this.createDate;
    }
    
    public void setCreateDate(Date date)
    {
    	this.createDate = date;
    }
    
    public Date getUpdateDate()
    {
    	return this.updateDate;
    }
    
    public void setUpdateDate(Date date)
    {
    	this.updateDate = date;
    }
    
    public Text getContents()
    {
    	return this.content;
    }
    
    public void setContents(Text content)
    {
    	this.content = content;
    }
    
    public int getUpdateCounts()
    {
    	return this.updateCounts;
    }
    
    public void setUpdateCounts(int counts)
    {
    	this.updateCounts = counts;
    }
    
    public static String test()
    {
    	
    	//PersistenceManager pm = JDOHelper.getPersistenceManagerFactory("transactions-optional").getPersistenceManager();
    	PersistenceManager pm = PersistenceManagerGenerator.getPersistentceManagerFactory().getPersistenceManager();
    	TablePage tp = new TablePage("test", "chencai", new Date(), new Text("it is just for test"));
        String ret = "ret:";
    	
    	
  
        pm.makePersistent(tp);
        
  
        
        String query = "select from " + TablePage.class.getName();
        List<TablePage> pages = (List<TablePage>) pm.newQuery(query).execute();
    	ret += pages.size();
        
        for(TablePage tpg : pages)
    	{
    		ret += "\n****************\n\t" + tpg.getPageID() + "\n\t" + tpg.getAuthor()+ " \n\t" + tpg.getTitle() + " \n\t" + tpg.getCreateDate().toString() + "\n\t " + tpg.getContents();
    		tpg.setAuthor("what");
    		pm.makePersistent(tpg);
    		
    		
    	}
    	
    	pm.close();
    	
    	
	    	
  
        
        return ret;
    }
    
    
    
    
    
}
